<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Boot Thymeleaf + Spring Security</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div class="container">
    <div class="starter-template">
        <h1>websoket 示例</h1>
        <h2>服务器时间：<p id="showtime"></p></h2>
    </div>
</div>
<script th:inline="javascript">
    var url = [[${cid}]];
    var socket;
    if (typeof(WebSocket) == "undefined") {
        console.log("您的浏览器不支持WebSocket");
    } else {
        console.log("您的浏览器支持WebSocket");
        //实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
        socket = new WebSocket(("ws://localhost:8080/websocket/" + url));
        socket.onopen = function () {
            document.getElementById('showtime').innerText = 'Socket 已打开';
        };
        socket.onmessage = function (msg) {
            document.getElementById('showtime').innerText = msg.data;
        };
        socket.onclose = function () {
            document.getElementById('showtime').innerText = 'Socket已关闭';
        };
        socket.onerror = function () {
            document.getElementById('showtime').innerText = 'Socket发生了错误';
        }
        //离开页面时，关闭socket
    }
</script>
</body>
</html>